<?php
/**
 * Created by PhpStorm.
 * User: qishengqiang
 * Date: 2021-04-28
 * Time: 16:39
 */

namespace app\applet\controller;


use app\applet\BaseController;

class UserAddressController extends BaseController
{
    /**
     * @return \think\response\Json
     */
    public function indexAction()
    {
        try {
            if (!$this->user) {
                throw new \Exception('请先登录', 401);
            }

            $select = [
                'where' => [
                    ['userId', '=', $this->user->userId]
                ],
                'order' => [
                    'isDefault' => 'desc',
                    'addressId' => 'desc'
                ]
            ];

            $addresses = $this->app->userAddressService->getAll($select);

            $data = [];

            if (count($addresses)) {
                foreach ($addresses as $address) {
                    $data[] = [
                        'addressId' => $address->addressId,
                        'consignee' => $address->consignee,
                        'phone' => $address->phone,
                        'provinceCode' => $address->provinceCode,
                        'provinceName' => $address->province()->value('provinceName'),
                        'cityCode' => $address->cityCode,
                        'cityName' => $address->city()->value('cityName'),
                        'areaCode' => $address->areaCode,
                        'areaName' => $address->area()->value('areaName'),
                        'addressName' => $address->province()->value('provinceName'). $address->city()->value('cityName').$address->area()->value('areaName').$address->address,
                        'address'=>$address->address,
                        'isDefault' => $address->isDefault
                    ];
                }
            }

            return json()->data(['code' => 200, 'msg' => '查询成功', 'data' => $data]);
        } catch (\Exception $e) {
            return json()->data(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data' => []]);
        }
    }

    public function detailAction($id)
    {
        try {
            if (!$this->user) {
                throw new \Exception('请先登录', 401);
            }

            $address = $this->app->userAddressService->getDetail($id);

            $data = [
                'addressId' => $address->addressId,
                'consignee' => $address->consignee,
                'phone' => $address->phone,
                'provinceCode' => $address->provinceCode,
                'provinceName' => $address->province()->value('provinceName'),
                'cityCode' => $address->cityCode,
                'cityName' => $address->city()->value('cityName'),
                'areaCode' => $address->areaCode,
                'areaName' => $address->area()->value('areaName'),
                'address' => $address->address,
                'isDefault' => $address->isDefault
            ];

            return json()->data(['code' => 200, 'msg' => '查询成功', 'data' => $data]);
        } catch (\Exception $e) {
            return json()->data(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data' => []]);
        }
    }

    /**
     * @return \think\response\Json
     */
    public function addAction()
    {
        try {
            if (!$this->user) {
                throw new \Exception('请先登录', 401);
            }

            $params = $this->post;
            $params['userId'] = $this->user->userId;

            $province = $this->app->commonService->getProvinceByName($this->post['provinceName']);
            $params["provinceCode"] = $province->provinceCode;

            $city = $this->app->commonService->getCityByName($this->post['cityName'], $province->provinceId);
            $params["cityCode"] = $city->cityCode;

            $area = $this->app->commonService->getAreaByName($this->post['areaName'], $city->cityId);
            $params["areaCode"] = $area->areaCode;

            $this->app->userAddressService->create($params);

            return json()->data(['code' => 200, 'msg' => '操作成功', 'data' => []]);
        } catch (\Exception $e) {
            return json()->data(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data' => []]);
        }
    }

    /**
     * @param $id
     * @return \think\response\Json
     */
    public function updateAction($id)
    {
        try {
            if (!$this->user) {
                throw new \Exception('请先登录', 401);
            }

            $address = $this->app->userAddressService->getDetail($id);

            $params = $this->post;

            $province = $this->app->commonService->getProvinceByName($this->post['provinceName']);
            $params["provinceCode"] = $province->provinceCode;

            $city = $this->app->commonService->getCityByName($this->post['cityName'], $province->provinceId);
            $params["cityCode"] = $city->cityCode;

            $area = $this->app->commonService->getAreaByName($this->post['areaName'], $city->cityId);
            $params["areaCode"] = $area->areaCode;

            $this->app->userAddressService->update($address, $params);

            return json()->data(['code' => 200, 'msg' => '操作成功', 'data' => []]);
        } catch (\Exception $e) {
            return json()->data(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data' => []]);
        }
    }

    public function deleteAction($id)
    {
        try {
            if (!$this->user) {
                throw new \Exception('请先登录', 401);
            }

            $address = $this->app->userAddressService->getDetail($id);

            $this->app->userAddressService->delete($address);

            return json()->data(['code' => 200, 'msg' => '操作成功', 'data' => []]);
        } catch (\Exception $e) {
            return json()->data(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data' => []]);
        }
    }

    public function defaultAction($id)
    {
        try {
            if (!$this->user) {
                throw new \Exception('请先登录', 401);
            }

            $address = $this->app->userAddressService->getDetail($id);

            $this->app->userAddressService->setDefault($address, $this->post['isDefault']);

            return json()->data(['code' => 200, 'msg' => '操作成功', 'data' => []]);
        } catch (\Exception $e) {
            return json()->data(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data' => []]);
        }
    }

    public function getDefaultAddressAction()
    {
        try {
            if (!$this->user) {
                throw new \Exception('请先登录', 401);
            }

            $address = $this->app->userAddressService->getDefaultAddress($this->user);

            $data = [];

            if ($address) {
                $data = [
                    'addressId' => $address->addressId,
                    'consignee' => $address->consignee,
                    'phone' => $address->phone,
                    'provinceCode' => $address->provinceCode,
                    'provinceName' => $address->province()->value('provinceName'),
                    'cityCode' => $address->cityCode,
                    'cityName' => $address->city()->value('cityName'),
                    'areaCode' => $address->areaCode,
                    'areaName' => $address->area()->value('areaName'),
                    'address' => $address->address,
                    'isDefault' => $address->isDefault
                ];
            }

            return json()->data(['code' => 200, 'msg' => '操作成功', 'data' => $data]);
        } catch (\Exception $e) {
            return json()->data(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data' => []]);
        }
    }
}